﻿using Abp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Abp.Migration
{
    public class MySqlAnnotationProvider : IAnnotationProvider
    {
        public string GetColumnAnnotation(string schema, string tableName, string columnName, string description)
        {
            throw new NotImplementedException();
        }

        public string GetTableAnnotation(string schema, string tableName, string description)
        {
            return string.Format("ALTER TABLE {0} COMMENT='{1}';  ",
                WrapperWith(GetTableName(schema, tableName)),
                description);
           
        }

        private string WrapperWith(string orgin, string start = "`", string end = "`")
        {
            if (string.IsNullOrWhiteSpace(orgin))
                return string.Empty;
            if (!orgin.StartsWith(start))
                orgin = start + orgin;
            if (!orgin.EndsWith(end))
                orgin = orgin + end;
            return orgin;
        }

        private string GetTableName(string schemaName, string tableName)
        {
            string result = WrapperWith(schemaName);
            if (string.IsNullOrWhiteSpace(result))
                result = WrapperWith(tableName);
            else
                result += "_" + WrapperWith(tableName);
            return result;

        }
    }
}
